package com.jxs.android.sxyw;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.xmlpull.v1.XmlPullParserException;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.jxs.android.soap.BuWebService;
import com.jxs.android.soap.IWebService;
import com.jxs.android.sxyw.boot.SelfApplication;
import com.jxs.android.sxyw.data.CaseInfo;
import com.jxs.android.sxyw.data.OrderType;
import com.jxs.android.sxyw.weiget.PopMsg;
import com.jxs.android.sxyw.weiget.TopBar;
import com.jxs.android.util.StartHelper;
import com.jxs.android.util.StringUtils;

/**
 * @Des 代维人员处理
 * @author Jiangxs
 * 
 */
public class CaseProcess extends AbstractActivity implements OnClickListener {

	private CaseInfo caseInfo;
	private Button submit;
	private EditText equipment_company, equipment_no, equipment_process;
	private LinearLayout equipment_company_ll, equipment_no_ll;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.case_process);

		if (null == SelfApplication.getUserInfo() || StringUtils.isEmpety(SelfApplication.getUserInfo().getTicket())) {
			PopMsg.show("登录已过期,请重新登录");
			StartHelper.startActivity(Login.class);
			return;
		}

		equipment_company = (EditText) findViewById(R.id.equipment_company);
		equipment_no = (EditText) findViewById(R.id.equipment_no);
		equipment_process = (EditText) findViewById(R.id.equipment_process);
		submit = (Button) findViewById(R.id.sumbit);

		equipment_company_ll = (LinearLayout) findViewById(R.id.equipment_company_ll);
		equipment_no_ll = (LinearLayout) findViewById(R.id.equipment_no_ll);

		if (null != getIntent() && null != getIntent().getExtras()) {
			caseInfo = (CaseInfo) getIntent().getExtras().get("caseInfo");
			if (caseInfo.getOrderType().index == OrderType.CASE_TASK.index) {
				equipment_company_ll.setVisibility(View.GONE);
				equipment_no_ll.setVisibility(View.GONE);
			}
		}

		submit.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		if (v.getId() == submit.getId()) {
			if (caseInfo.getOrderType().index == OrderType.CASE_ALARM.index) {
				if (StringUtils.isEmpety(equipment_company.getText().toString())) {
					PopMsg.show("请填写设备厂家.");
					return;
				}
				if (StringUtils.isEmpety(equipment_no.getText().toString())) {
					PopMsg.show("请填写设备型号.");
					return;
				}
			}
			if (StringUtils.isEmpety(equipment_process.getText().toString())) {
				PopMsg.show("请填写处理过程.");
				return;
			}
			sumbit(SelfApplication.getUserInfo().getTicket(), caseInfo);
		}
	}

	private void sumbit(final String ticket, final CaseInfo caseInfo) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				String wsdl_url = SelfApplication.WSDL_URL;// wsdl
				String namespace = SelfApplication.WSDL_NAME_SPACE;// namespace
				String methodName = "processWorkOrderToReply3";// 要调用的方法名称
				SoapObject request = new SoapObject(namespace, methodName);
				request.addProperty("ticket", ticket);
				request.addProperty("case_no", caseInfo.getCase_no());
				request.addProperty("processDeviceCompany", equipment_company.getText().toString() + "");
				request.addProperty("processDeviceTypeNo", equipment_no.getText().toString() + "");
				request.addProperty("processText", equipment_process.getText().toString() + "");
				request.addProperty("workOrderType", caseInfo.getOrderType().index + "");

				BuWebService webService = new BuWebService(wsdl_url, request);
				webService.setDoNet(false);
				webService.requestWSDL(new IWebService() {
					@Override
					public void logic(SoapSerializationEnvelope envelope) throws IOException, XmlPullParserException {
						paserSubmit(envelope.getResponse().toString());
					}
				});
			}
		}).start();
	}

	private void paserSubmit(String stauts) {
		Message msg = new Message();
		msg.what = RESULT;
		msg.obj = stauts;
		handler.sendMessage(msg);
	}

	private int RESULT = 0x98765;

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == RESULT) {
				String status = msg.obj.toString();
				if ("0".equals(status)) {
					setResult(RESULT_OK);
					finish();
				} else if ("-1".equals(status)) {
					PopMsg.show("登录已过期,请重新登录");
					StartHelper.startActivity(Login.class);
					finish();
				} else if ("-2".equals(status)) {
					PopMsg.show("服务端异常");
				} else if ("-3".equals(status)) {
					PopMsg.show("输入参数无效");
				}
			}
		}

	};

	@Override
	public void topBarLogic(TopBar topBar) {
		topBar.setTitle(R.string.work_order_reply);
		initTopBarReturn(topBar);
		topBar.hindBtn();
	}

}
